package ahblue.cn.tanzi.receiver;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;


import java.io.File;

import ahblue.cn.tanzi.util.SharePreferenceUtil;

/**
 * Created by Administrator on 2016/9/20.
 */

public class UpdataBroadcastReceiver extends BroadcastReceiver {
    SharePreferenceUtil sharePreferenceUtil;
    @SuppressLint("NewApi")
    public void onReceive(Context context, Intent intent) {
        long downLoadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        sharePreferenceUtil = new SharePreferenceUtil(context);
        long cacheDownLoadId = sharePreferenceUtil.getDownloadId();
        if (cacheDownLoadId == downLoadId) {
            install(context);
        }
    }


    private void install(Context context){
        Intent install = new Intent(Intent.ACTION_VIEW);
        File apkFile = queryDownloadedApk(context);
        if(apkFile!=null){
            if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N) {

                Uri contentUri = FileProvider.getUriForFile(context,"ahblue.cn.tanzi.fileprovider",apkFile);
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                install.setDataAndType(contentUri,"application/vnd.android.package-archive");
                context.startActivity(install);
            }else{
                install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(install);
            }
//            install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
//            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(install);
        }

    }

    //通过downLoadId查询下载的apk，解决6.0以后安装的问题
    public File queryDownloadedApk(Context context) {
        File targetApkFile = null;
        DownloadManager downloader = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        long downloadId = sharePreferenceUtil.getDownloadId();
        if (downloadId != -1) {
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(downloadId);
            query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
            Cursor cur = downloader.query(query);
            if (cur != null) {
                if (cur.moveToFirst()) {
                    String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    if (!TextUtils.isEmpty(uriString)) {
                        targetApkFile = new File(Uri.parse(uriString).getPath());
                    }
                }
                cur.close();
            }
        }
        return targetApkFile;
    }
}